package com.mathassistant.client.presenter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.mathassistant.client.event.ShowAboutDialogEvent;
import com.mathassistant.client.event.ShowLoginDialogEvent;
import com.mathassistant.client.event.ToAdminViewEvent;
import com.mathassistant.client.event.ToAdminViewEventHandler;
import com.mathassistant.client.event.ToUserViewEvent;
import com.mathassistant.client.event.ToUserViewEventHandler;

public class FooterPresenter implements Presenter {

	public interface Display {
		HasClickHandlers getLoginAnchor();

		HasClickHandlers getLogoutAnchor();

		HasClickHandlers getAboutAnchor();

		void setToAdminView(String userName);

		void setToUserView();

		Widget asWidget();
	}

	private final Display display;
	private final SimpleEventBus eventBus;

	public FooterPresenter(SimpleEventBus eventBus, Display view) {
		this.eventBus = eventBus;
		this.display = view;
		bind();
	}

	public Display getDisplay() {
		return display;
	}

	private void bind() {
		// TODO Auto-generated method stub
		display.getAboutAnchor().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				eventBus.fireEvent(new ShowAboutDialogEvent());
			}
		});
		display.getLoginAnchor().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				eventBus.fireEvent(new ShowLoginDialogEvent());
			}
		});
		display.getLogoutAnchor().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				eventBus.fireEvent(new ToUserViewEvent());
			}
		});

		eventBus.addHandler(ToAdminViewEvent.TYPE,
				new ToAdminViewEventHandler() {

					@Override
					public void onTopicListToAdminView(ToAdminViewEvent event) {
						// TODO Auto-generated method stub
						display.setToAdminView("Ch\u00E0o "
								+ event.getUser().getName() + " :");
					}
				});

		eventBus.addHandler(ToUserViewEvent.TYPE, new ToUserViewEventHandler() {

			@Override
			public void onTopicListToUserView(ToUserViewEvent event) {
				// TODO Auto-generated method stub
				display.setToUserView();
			}
		});
	}

	@Override
	public void go(HasWidgets container) {
		// TODO Auto-generated method stub
		container.clear();
		container.add(display.asWidget());
	}

	public void go() {

	}
}
